$(function(){
    var allDiv = document.getElementById('allContent');
    var unPayDiv = document.getElementById('unPayContent');
    var unShipDiv = document.getElementById('unShipContent');
    var unGetDiv = document.getElementById('unGetContent');

    $('#unPay').on('click',function() {
        console.log("unpay click");
        allDiv.style.backgroundColor = '#dfeae4';
        unPayDiv.style.backgroundColor = '#146e34';
        unShipDiv.style.backgroundColor = '#dfeae4';
        unGetDiv.style.backgroundColor = '#dfeae4';
        $.ajax({
            type: 'GET',
            url: 'http://localhost:8080/user/findNoPayOrder',
            success: function (data) {
                console.log("data");
                data = data.data;
                console.log(data);
                var productListHTML = '<tr>\n' +
                    '                    <th>订 单 号</th>\n' +
                    '                    <th>下单时间</th>\n' +
                    '                    <th>总 价</th>\n' +
                    '                    <th>付款状态</th>\n' +
                    '                    <th>发货状态</th>\n' +
                    '                    <th>收货状态</th>\n' +
                    '                    <th>操 作</th>\n' +
                    '                    </tr>';
                if(data.length === 0){
                    productListHTML += '<tr><td colspan="7">Your order information is empty.</td></tr>';
                }else{
                    for (var i = 0; i < data.length; i++) {
                        productListHTML += '<tr>';
                        productListHTML += '<td><a href="/user/orderForm?orderId=';
                        productListHTML += data[i].orderid;
                        productListHTML += '">';
                        productListHTML += data[i].orderid;
                        productListHTML += '</a></td>';
                        productListHTML += '<td>';
                        productListHTML += data[i].orderDate;
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        productListHTML += data[i].totalprice;
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].ispay)
                            productListHTML += '已付款';
                        else
                            productListHTML += '未付款';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].issend)
                            productListHTML += '已发货';
                        else
                            productListHTML += '未发货';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].isget)
                            productListHTML += '已收货';
                        else
                            productListHTML += '未收货';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if(data[i].isreturn)
                            productListHTML += '已退货';
                        else {
                            if(!data[i].isget){
                                productListHTML += '<a id="goCancel" th:href="/user/deleteOrder?orderId="';
                                productListHTML += data[i].orderid;
                                productListHTML += ' class="Button">去退货</a>';
                            }
                            else{
                                productListHTML += '<span>已完成</span>';
                            }
                        }
                        productListHTML += '</td>';
                        productListHTML += '</tr>';
                    }
                }
                console.log(productListHTML);
                $('#orderTable').html(productListHTML);
                // $('#productAutoComplete').show();
                // $('#productAutoItem').show();
            },
            error: function (errorMsg) {
                console.log(errorMsg);
            }
        });
    });
    $('#all').on('click',function() {
        allDiv.style.backgroundColor = '#146e34';
        unPayDiv.style.backgroundColor = '#dfeae4';
        unShipDiv.style.backgroundColor = '#dfeae4';
        unGetDiv.style.backgroundColor = '#dfeae4';
        console.log("all click");
        $.ajax({
            type: 'GET',
            url: 'http://localhost:8080/user/findAllOrder',
            success: function (data) {
                console.log("data");
                data = data.data;
                console.log(data);
                var productListHTML = '<tr>\n' +
                    '                    <th>订 单 号</th>\n' +
                    '                    <th>下单时间</th>\n' +
                    '                    <th>总 价</th>\n' +
                    '                    <th>付款状态</th>\n' +
                    '                    <th>发货状态</th>\n' +
                    '                    <th>收货状态</th>\n' +
                    '                    <th>操 作</th>\n' +
                    '                    </tr>';
                if(data.length === 0){
                    productListHTML += '<tr><td colspan="7">Your order information is empty.</td></tr>';
                }else {
                    for (var i = 0; i < data.length; i++) {
                        productListHTML += '<tr>';
                        productListHTML += '<td><a href="/user/orderForm?orderId=';
                        productListHTML += data[i].orderid;
                        productListHTML += '">';
                        productListHTML += data[i].orderid;
                        productListHTML += '</a></td>';
                        productListHTML += '<td>';
                        productListHTML += data[i].orderDate;
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        productListHTML += data[i].totalprice;
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].ispay)
                            productListHTML += '已付款';
                        else
                            productListHTML += '未付款';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].issend)
                            productListHTML += '已发货';
                        else
                            productListHTML += '未发货';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].isget)
                            productListHTML += '已收货';
                        else
                            productListHTML += '未收货';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].isreturn)
                            productListHTML += '已退货';
                        else {
                            if (data[i].isget) {
                                productListHTML += '<span>已完成</span>';
                            } else {
                                productListHTML += '<a id="goCancel" th:href="/user/deleteOrder?orderId="';
                                productListHTML += data[i].orderid;
                                productListHTML += ' class="Button">去退货</a>';
                            }
                        }
                        productListHTML += '</td>';
                        productListHTML += '</tr>';
                    }
                }
                console.log(productListHTML);
                $('#orderTable').html(productListHTML);
                // $('#productAutoComplete').show();
                // $('#productAutoItem').show();
            },
            error: function (errorMsg) {
                console.log(errorMsg);
            }
        });
    });
    $('#unShip').on('click',function() {
        allDiv.style.backgroundColor = '#dfeae4';
        unPayDiv.style.backgroundColor = '#dfeae4';
        unShipDiv.style.backgroundColor = '#146e34';
        unGetDiv.style.backgroundColor = '#dfeae4';
        console.log("unShip click");
        $.ajax({
            type: 'GET',
            url: 'http://localhost:8080/user/findNoShipOrder',
            success: function (data) {
                console.log("data");
                data = data.data;
                console.log(data);
                var productListHTML = '<tr>\n' +
                    '                    <th>订 单 号</th>\n' +
                    '                    <th>下单时间</th>\n' +
                    '                    <th>总 价</th>\n' +
                    '                    <th>付款状态</th>\n' +
                    '                    <th>发货状态</th>\n' +
                    '                    <th>收货状态</th>\n' +
                    '                    <th>操 作</th>\n' +
                    '                    </tr>';
                if(data.length === 0){
                    productListHTML += '<tr><td colspan="7">Your order information is empty.</td></tr>';
                }else {
                    for (var i = 0; i < data.length; i++) {
                        productListHTML += '<tr>';
                        productListHTML += '<td><a href="/user/orderForm?orderId=';
                        productListHTML += data[i].orderid;
                        productListHTML += '">';
                        productListHTML += data[i].orderid;
                        productListHTML += '</a></td>';
                        productListHTML += '<td>';
                        productListHTML += data[i].orderDate;
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        productListHTML += data[i].totalprice;
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].ispay)
                            productListHTML += '已付款';
                        else
                            productListHTML += '未付款';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].issend)
                            productListHTML += '已发货';
                        else
                            productListHTML += '未发货';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].isget)
                            productListHTML += '已收货';
                        else
                            productListHTML += '未收货';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].isreturn)
                            productListHTML += '已退货';
                        else {
                            if(data[i].isget) {
                                productListHTML += '<span>已完成</span>';
                            }else {
                                productListHTML += '<a id="goCancel" th:href="/user/deleteOrder?orderId="';
                                productListHTML += data[i].orderid;
                                productListHTML += ' class="Button">去退货</a>';
                            }
                        }
                        productListHTML += '</td>';
                        productListHTML += '</tr>';
                    }
                }
                console.log(productListHTML);
                $('#orderTable').html(productListHTML);
                // $('#productAutoComplete').show();
                // $('#productAutoItem').show();
            },
            error: function (errorMsg) {
                console.log(errorMsg);
            }
        });
    });
    $('#unGet').on('click',function() {
        console.log("unGet click");
        allDiv.style.backgroundColor = '#dfeae4';
        unPayDiv.style.backgroundColor = '#dfeae4';
        unShipDiv.style.backgroundColor = '#dfeae4';
        unGetDiv.style.backgroundColor = '#146e34';
        $.ajax({
            type: 'GET',
            url: 'http://localhost:8080/user/findNoGetOrder',
            success: function (data) {
                console.log("data");
                data = data.data;
                console.log(data);
                var productListHTML = '<tr>\n' +
                    '                    <th>订 单 号</th>\n' +
                    '                    <th>下单时间</th>\n' +
                    '                    <th>总 价</th>\n' +
                    '                    <th>付款状态</th>\n' +
                    '                    <th>发货状态</th>\n' +
                    '                    <th>收货状态</th>\n' +
                    '                    <th>操 作</th>\n' +
                    '                    </tr>';
                if(data.length === 0){
                    productListHTML += '<tr><td colspan="7">Your order information is empty.</td></tr>';
                }else {
                    for (var i = 0; i < data.length; i++) {
                        productListHTML += '<tr>';
                        productListHTML += '<td><a href="/user/orderForm?orderId=';
                        productListHTML += data[i].orderid;
                        productListHTML += '">';
                        productListHTML += data[i].orderid;
                        productListHTML += '</a></td>';
                        productListHTML += '<td>';
                        productListHTML += data[i].orderDate;
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        productListHTML += data[i].totalprice;
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].ispay)
                            productListHTML += '已付款';
                        else
                            productListHTML += '未付款';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].issend)
                            productListHTML += '已发货';
                        else
                            productListHTML += '未发货';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].isget)
                            productListHTML += '已收货';
                        else
                            productListHTML += '未收货';
                        productListHTML += '</td>';
                        productListHTML += '<td>';
                        if (data[i].isreturn)
                            productListHTML += '已退货';
                        else {
                            if (data[i].isget) {
                                productListHTML += '<span>已完成</span>';
                            } else {
                                productListHTML += '<a id="goCancel" th:href="/user/deleteOrder?orderId="';
                                productListHTML += data[i].orderid;
                                productListHTML += ' class="Button">去退货</a>';
                            }
                        }
                        productListHTML += '</td>';
                        productListHTML += '</tr>';
                    }
                }
                console.log(productListHTML);
                $('#orderTable').html(productListHTML);
                // $('#productAutoComplete').show();
                // $('#productAutoItem').show();
            },
            error: function (errorMsg) {
                console.log(errorMsg);
            }
        });
    });
});
